package org.realogic.cfm.common.collection;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class OrderedSet<E> extends AbstractSet<E> {

	private List<E> backedList = new LinkedList<E>();

	public OrderedSet() {
	}

	public OrderedSet(Collection<E> c) {
		Iterator<E> i = c.iterator();

		while (i.hasNext()) {
			add(i.next());
		}
	}

	public Iterator<E> iterator() {
		return backedList.iterator();
	}

	public int size() {
		return backedList.size();
	}

	public boolean add(E obj) {
		int index = backedList.indexOf(obj);

		if (index == -1) {
			return backedList.add(obj);
		} else {
			backedList.set(index, obj);
			return false;
		}
	}
}
